﻿using System.Security.Cryptography;
using System.Text;

namespace Web7.Utilities
{
    public sealed class EncryptManager
    {
        /// <summary>
        /// iv
        /// </summary>
        private static readonly string IVStringForPassword = "7RCcUDNagvI=";

        /// <summary>
        /// key
        /// </summary>
        private static readonly string KeyStringForPassword = "2UkoifGvu4T1XT1BRFMiuhOYTBZPEKE9";

        public static string Decrypt(SymmetricEncryptType encryptType, string encryptedString)
        {
            SymmetricEncrypt encrypt = new SymmetricEncrypt(encryptType)
            {
                IVString = IVStringForPassword,
                KeyString = KeyStringForPassword
            };
            return encrypt.Decrypt(encryptedString);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="encryptType"></param>
        /// <param name="encryptedString"></param>
        /// <param name="ivString"></param>
        /// <param name="keyString"></param>
        /// <returns></returns>
        public static string Decrypt(SymmetricEncryptType encryptType, string encryptedString, string ivString, string keyString)
        {
            SymmetricEncrypt encrypt = new SymmetricEncrypt(encryptType)
            {
                IVString = ivString,
                KeyString = keyString
            };
            return encrypt.Decrypt(encryptedString);
        }

        /// <summary>
        /// 密码解密令牌
        /// </summary>
        /// <param name="encryptedString"></param>
        /// <returns></returns>
        public static string DecryptTokenForPassword(string encryptedString)
        {
            return Decrypt(SymmetricEncryptType.TripleDES, encryptedString, IVStringForPassword, KeyStringForPassword);
        }

        /// <summary>
        /// 采用系统默认加密
        /// </summary>
        /// <param name="encryptType"></param>
        /// <param name="originalString"></param>
        /// <returns></returns>
        public static string Encrypt(SymmetricEncryptType encryptType, string originalString)
        {
            SymmetricEncrypt encrypt = new SymmetricEncrypt(encryptType)
            {
                IVString = IVStringForPassword,
                KeyString = KeyStringForPassword
            };
            return encrypt.Encrypt(originalString);
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="encryptType"></param>
        /// <param name="originalString"></param>
        /// <param name="ivString"></param>
        /// <param name="keyString"></param>
        /// <returns></returns>
        public static string Encrypt(SymmetricEncryptType encryptType, string originalString, string ivString, string keyString)
        {
            SymmetricEncrypt encrypt = new SymmetricEncrypt(encryptType)
            {
                IVString = ivString,
                KeyString = keyString
            };
            return encrypt.Encrypt(originalString);
        }

        /// <summary>
        /// 密码加密令牌
        /// </summary>
        /// <param name="originalString"></param>
        /// <returns></returns>
        public static string EncryptTokenForPassword(string originalString)
        {
            return Encrypt(SymmetricEncryptType.TripleDES, originalString, IVStringForPassword, KeyStringForPassword);
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string MD5(string str)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            bytes = new MD5CryptoServiceProvider().ComputeHash(bytes);
            string str2 = "";
            for (int i = 0; i < bytes.Length; i++)
            {
                str2 = str2 + bytes[i].ToString("x").PadLeft(2, '0');
            }
            return str2;
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string MD5_16(string str)
        {
            return MD5(str).Substring(8, 0x10);
        }

    }
}
